// LEC    March 26, 2021
//
// STATE SIZE PAPER
// Analysis file




cd $OUTDIR

// PREPARE TABLE 6: NUMER OF BORDER CHANGE EVENTS AS SAHRE OF ALL EVENTS

use ethbc, clear


tab birtha deathb

tab birtha deathb if ethchange==1


cd $OUTDIR
use analysis_data, clear


drop if year<$STARTYEAR
gen id1886 = .
replace id1886 = 1 if oldstate==0

bys year: egen n1 = count(id) if id1886 != .
bys year: egen n2 = count(id) if id1886 == .

gen area1 = .
replace area1 = area if id1886 != . 

gen area2 = .
replace area2 = area if id1886 == . & n2>10

gen maxshare1 = .
replace maxshare1 = maxshare if id1886 != .

gen maxshare2 = .
replace maxshare2 = maxshare if id1886 == .

gen ef1 = .
replace ef1 = ef if id1886 != .

gen ef2 = .
replace ef2 = ef if id1886 == . & n2>10

gen agfrac1 = .
replace agfrac1 = agfrac if id1886 != .

gen agfrac2 = .
replace agfrac2 = agfrac if id1886 == . & n2>10

bys year: egen sumarea = sum(area)

bys year: egen sysconc = sum((area/sumarea)^2)


// Weighted means

bys year: egen efaw = sum(area*ef)
replace efaw = efaw/sumarea

bys year: egen agfracaw = sum(area*agfrac)
replace agfracaw = agfracaw/sumarea


bys year: egen maxshareaw = sum(area*maxshare)
replace maxshareaw = maxshareaw/sumarea

bys year: egen maxagshareaw = sum(area*maxagshare)
replace maxagshareaw = maxagshareaw/sumarea


bys year: egen intdjaw = sum(area*intdj)
replace intdjaw = intdjaw/sumarea

bys year: egen extdjaw = sum(area*extdj)
replace extdjaw = extdjaw/sumarea




bys year: egen sumarea1 = sum(area*oldstate)
bys year: egen sumarea2 = sum(area*(1-oldstate))

bys year: egen efaw1 = sum(area*ef*oldstate)
replace efaw1 = efaw1/sumarea1

bys year: egen agfracaw1 = sum(area*agfrac*oldstate)
replace agfracaw1 = agfracaw1/sumarea1

bys year: egen efaw2 = sum(area*ef*(1-oldstate))
replace efaw2 = efaw2/sumarea2

bys year: egen agfracaw2 = sum(area*agfrac*(1-oldstate))
replace agfracaw2 = agfracaw2/sumarea2



bys year: egen sumpop0=sum(population)

bys year: egen efpw = sum(population*ef)
replace efpw = efpw/sumpop0

bys year: egen agfracpw = sum(population*agfrac)
replace agfracpw = agfracpw/sumpop0

bys year: egen sumpop1 = sum(population*oldstate)
bys year: egen sumpop2 = sum(population*(1-oldstate))

bys year: egen efpw1 = sum(population*ef*oldstate)
replace efpw1 = efpw1/sumpop1

bys year: egen agfracpw1 = sum(population*agfrac*oldstate)
replace agfracpw1 = agfracpw1/sumpop1

bys year: egen efpw2 = sum(population*ef*(1-oldstate))
replace efpw2 = efpw2/sumpop2

bys year: egen agfracpw2 = sum(population*agfrac*(1-oldstate))
replace agfracpw2 = agfracpw2/sumpop2

bys year: egen syspopconc = sum((population/sumpop0)^2)


sort idc year
xtset idc year

bys year: egen medarea = median(area)


//////////// Collapse data to system level ///////////////////////////////////////////////////


collapse (max)n n1 n2 (mean)sumarea area medarea area1 area2 lnarea maxshare maxshare1 maxshare2 ///
(mean)ef ef1 ef2 agfrac agfrac1 agfrac2 efaw efaw1 efaw2 agfracaw agfracaw1 agfracaw2 efpw agfracpw efpw1 agfracpw1 efpw2 agfracpw2 maxagshare maxshareaw maxagshareaw ///
intdj intdjaw extdj extdjaw ///
(mean)sysconc syspopconc /*(mean)wrsysconc*/ (sum)terrnull transfera absorba absorbb secessiona secessionb collapsea collapseb ethseca ethsecb irra ethabsorbb ///
(sum) ethcollapsea ethcollapseb, by (year) ///
// cumularea lncumularea cumuletharea lncumuletharea cumulnetharea lncumulnetharea


drop if year<$STARTYEAR


tset year

gen bc = transfera + absorbb + secessiona + collapsea
gen ethbc = irra + ethabsorbb + ethseca + ethcollapseb

gen neabsorb = absorbb - ethabsorbb
gen nesec = secessiona - ethseca
gen necollapsea = collapsea - ethcollapsea
gen necollapseb = collapseb - ethcollapseb

// net border change
gen dn = secessiona + collapsea - absorbb -collapseb + terrnull
gen ethdn = ethseca + ethcollapsea - ethabsorbb - ethcollapseb + terrnull
gen nethdn = nesec + necollapsea - neabsorb - necollapseb + terrnull

gen one = 1
bys one (year): gen cumuldn = sum(dn)
bys one (year): gen cumulethdn = sum(ethdn)
bys one (year): gen cumulnethdn = sum(nethdn)

gen startyear = 0
replace startyear = 1 if year == $STARTYEAR

bys one: egen n0 = max(startyear*n)
bys one: egen area0 = max(startyear*sumarea)

gen reln = n0 + cumuldn
gen delta = reln - n

gen totarea = sumarea/(n0 + cumuldn)
gen etharea = sumarea/(n0 + cumulethdn)
gen netharea = sumarea/(n0 + cumulnethdn)


gen neth = n0 + cumulethdn
gen nneth = n0 + cumulnethdn


/////////////////////////////////////////////////////////////////////////
/*
 // Export data for combined area graph (see code in analyais_system_bridging.do)
 cd $OUTPUTDIR
 gen  cshp_area = area
 save cshp_system.dta, replace
*/
////////////////////////////////////////////////////////////////////////


cd $OUTDIR
merge 1:1 year using ethnicvars2
drop _merge

 
global G xla(1880(20)2020)  graphregion(color(white)) bgcolor(white)
global G xla(1880(10)2020)  graphregion(color(white)) bgcolor(white)
global G2 xla(1850(50)2020)  graphregion(color(white)) bgcolor(white)
global R "(1880(20)2020)"
global R1 "(1880(10)2020)"   

 cd $OUTPUTDIR

 
// NOT USED (SEE BOMBINED SIZE GRAPH ABOVE)
/*
tsline area, yla(0(500000)2500000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size) ///
 graphregion(color(white)) bgcolor(white) scheme(s2mono)
graph export StateSize_World.png, width(1275) replace
graph export StateSize_World.pdf, replace
*/

// OUTPUT FIGURE A3: Trends in the number of states
tsline n, xla(1880(20)2020) yla(0(50)200) ytitle(Number of States) graphregion(color(white)) bgcolor(white) scheme(s2mono)
graph export NStates_World.png, width(1275) replace
graph export NStates_World.pdf, replace

// OUTPUT FIGURE 6: Trends in fractionalization
twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line efaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfracaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)
graph export FracGraph_World.png, width(1275) replace
graph export FracGraph_World.pdf, replace

// OUTPUT FIGURE 10: Trends in ethnic and non-ethnic state size
// OUTPUT FIGURE A6: SAME BUT WITHOUT DEPENDENCIES
tsline totarea etharea netharea, yla(0(1000000)3000000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size)  ///
legend(order(1 "Actual state size" 2 "Ethnic trend" 3 "Non-ethnic trend")) graphregion(color(white)) bgcolor(white) scheme(s2mono) 
graph export Trends_World.png, width(1275) replace
graph export Trends_World.pdf, replace


// NOT USED
/*
twoway (line efaw year, lwidth(thick) lpattern(solid) lcolor(red)) (line tfagpopaw year, lwidth(thick) lpattern(dash) lcolor(green)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) yla(0(0.1)0.7) ///
legend (order(1  "Ethnic fractionalization" 2 "Territorial fractionalization")) ///
ytitle()
// graph export FracGraph_Europe.png, width(1275) replace
graph export FracGraph_Europe.pdf, replace

 
 
twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line efaw year, yaxis(2)  lwidth(thick) lcolor(red) lpattern(solid)) (line agfracaw year, yaxis(2)  lwidth(thick) lcolor(green) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)
 */


////////////////////////////////////////////////////////////////////////
// Create bar charts
////////////////////////////////////////////////////////////////////////
   
gen year5 = int((year+2)/5)
//replace year = year5

collapse (sum)bc secessiona collapsea transfera absorbb ethseca irra ethabsorbb ethcollapsea, by (year5)


gen secession = .
gen sum2 = .
gen sum3 = .
replace secession = secessiona + collapsea
replace sum2 = secession + transfera
replace sum3 = sum2 + absorbb


/* NOT USED
cd $OUTPUTDIR
twoway bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xlab(376 "1880" 380 "1900" 384 "1920" 388 "1940" 392 "1960" 396 "1980" 400 "2000" 404 "2020") xtick(376(2)404)  ///
legend(order(1 "Absorption" 2 "Transfer" 3 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export BorderChange_World.png, width(1275) replace
graph export BorderChange_World.pdf, replace
*/

// OUTPUT FIGURE A9
twoway bar sum3 year, bcolor(midgreen) ///
|| bar sum2 year, bcolor(gold) ///
|| bar secession year, bcolor(cranberry) ///
, xlab(376 "1880" 380 "1900" 384 "1920" 388 "1940" 392 "1960" 396 "1980" 400 "2000" 404 "2020") xtick(376(2)404)  ///
legend(order(1 "Absorption" 2 "Transfer" 3 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export BorderChange_World.png, width(1275) replace
graph export BorderChange_World.pdf, replace

 
 
replace secession = ethseca + ethcollapsea
replace sum2 = secession + irra
replace sum3 = sum2 + ethabsorbb

/* NOT USED
cd $OUTPUTDIR
twoway bar bc year, bcolor(ltblue) ///
|| bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xlab(376 "1880" 380 "1900" 384 "1920" 388 "1940" 392 "1960" 396 "1980" 400 "2000" 404 "2020") xtick(376(2)404)  ///
legend(order(1 "Non-ethnic events" 2 "Unification" 3 "Irredentism" 4 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export EthBorderChange_World.png, width(1275) replace
graph export EthBorderChange_World.pdf, replace
*/

// OUTPUT FIGURE 8: Bar diagram with ethnic border change events

twoway bar bc year, bcolor(ltbluishgray) ///
|| bar sum3 year, bcolor(midgreen) ///
|| bar sum2 year, bcolor(gold) ///
|| bar secession year, bcolor(cranberry) ///
, xlab(376 "1880" 380 "1900" 384 "1920" 388 "1940" 392 "1960" 396 "1980" 400 "2000" 404 "2020") xtick(376(2)404)  ///
legend(order(1 "Non-ethnic events" 2 "Unification" 3 "Irredentism" 4 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export EthBorderChange_World.png, width(1275) replace
graph export EthBorderChange_World.pdf, replace



/*

xxx


tsline ef agfrac, xla(1880(20)2020) yla(0(0.1)0.4) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Territorial Frac. kin AG")) 

tsline efaw agfracaw, xla(1880(20)2020) yla(0(0.1)0.6) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Terr. Frac. Kin Group")) 

tsline efpw agfracpw, xla(1880(20)2020) yla(0(0.1)0.6) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Terr. Frac. Kin Group")) 

tsline efaw agfracaw, xla(1880(20)2020) yla(0(0.1)0.6) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Terr. Frac. Kin Group")) 



tsline efpw ef1 ef2, $G ///
legend(order(1 "All states" 2 "Old states" 3 "New states")) ytitle(Terr. Fractionalization) scheme(s2mono)

tsline efpw efpw1 efpw2, $G ///
legend(order(1 "All states" 2 "Old states" 3 "New states")) ytitle(Terr. Fractionalization) scheme(s2mono)

tsline agfracpw agfracpw1 agfracpw2, $G ///
legend(order(1 "All states" 2 "Old states" 3 "New states")) ytitle(Terr. Fractionalization) scheme(s2mono)


xxx
 tsline lntotarea lntotetharea lntotnetharea
 tsline totarea2 etharea2 netharea2, yla(0(1000000)3000000) xla(1880(20)2020)  ///
legend(order(1 "Actual area" 2 "Ethnic changes" 3 "Non-ethnic changes")) graphregion(color(white)) bgcolor(white) scheme(s2mono) 
 
 xxx
 
 // Export data for combined area graph
 cd "/Users/larsc/polybox/Shared/NASTACdb/StateSizeProject/OutputCShapesagg"
 rename area cshp_area
 save cshp_system.dta, replace
 
 
 
 tsline area, yla(0(500000)2500000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size) ///
 graphregion(color(white)) bgcolor(white) scheme(s2mono)

 
tsline dn, xla(1880(20)2020) graphregion(color(white)) bgcolor(white) scheme(s2mono) 

tsline ethdn, xla(1880(20)2020) graphregion(color(white)) bgcolor(white) scheme(s2mono) 

tsline nethdn, xla(1880(20)2020) graphregion(color(white)) bgcolor(white) scheme(s2mono) 

tsline totarea etharea netharea, yla(0(1000000)3000000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size)  ///
legend(order(1 "Actual state size" 2 "Ethnic trend" 3 "Non-ethnic trend")) graphregion(color(white)) bgcolor(white) scheme(s2mono) 
 

 
 
tsline area, yla(0(500000)2500000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size) ///
graphregion(color(white)) bgcolor(white) scheme(s2mono)

tsline n, xla(1880(20)2020) yla(0(50)200) ytitle(Number of States) graphregion(color(white)) bgcolor(white) scheme(s2mono)


twoway (line area year, yaxis(1)) (line efaw year, yaxis(2)) (line agfracaw year, yaxis(2)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Ethnic Frac." 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)




// color
twoway (line area year, yaxis(1) lwidth(thick)) (line efaw year, yaxis(2) lcolor(red) lwidth(thick)) (line agfracaw year, yaxis(2) lcolor(green) lwidth(thick)), /// 
graphregion(color(white)) bgcolor(white) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Ethnic Frac." 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size) 

// BW

twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line ef year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfrac year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)

twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line efaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfracaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line intdjaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line extdjaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Internal Disjunction" 3 "External Disjunction")) ///
ytitle(Mean State Size)


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line maxshareaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line maxagshareaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Internal share" 3 "External share")) ///
ytitle(Mean State Size)


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line maxshare year, yaxis(2)  lwidth(medium) lpattern(solid)) (line maxagshare year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Dominant share" 3 "Spillover share")) ///
ytitle(Mean State Size)


gen secession = .
gen sum2 = .
gen sum3 = .

replace secession = secessiona + collapsea
replace sum2 = secession + transfera
replace sum3 = sum2 + absorbb
twoway bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xla(1880(20)2020) xla(1880(20)2020)  ///
legend(order(1 "Absorption" 2 "Transfer" 3 "Secession & Collapse")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
 
 

replace sum2 = ethseca + irra
replace sum3 = sum2 + ethabsorbb
twoway bar bc year, bcolor(ltblue) ///
|| bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xla(1880(20)2020) xla(1880(20)2020) ///
legend(order(1 "Non-ethnic events" 2 "Unification" 3 "Irredentism" 4 "Secession & Collapse")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
  

twoway (line efaw year, lwidth(medium) lpattern(solid)) (line agfracaw year, lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) yla(0(0.1)0.6) ///
legend (order(1  "Ethnic Frac. Country" 2 "Terr. Frac. Main Group")) ///
ytitle()
  
  
twoway (line intdjaw year, lwidth(medium) lpattern(solid)) (line extdjaw year, lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) yla(0(0.1)0.7) ///
legend (order(1  "Internal disjunction" 2 "External disjunction")) ///
ytitle()  
  
  
twoway (line intdjaw year, lwidth(thick) lpattern(solid) lcolor(red)) (line extdjaw year, lwidth(thick) lpattern(dash) lcolor(green)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) yla(0(0.1)0.7) ///
legend (order(1  "Internal disjunction" 2 "External disjunction")) ///
ytitle()    
  
  
twoway (line intdj year, lwidth(medium) lpattern(solid)) (line extdj year, lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) yla(0(0.1)0.5) ///
legend (order(1  "Internal disjunction" 2 "External disjunction")) ///
ytitle() 
  
  
  */
